﻿using UnityEngine;

namespace com.yoozoo.gta.Tools.UI
{
    public class SpriteFrameRadar : MonoBehaviour
    {
        private SpriteListItem[] sprites;
        private SpriteListItem[] maskSprites;
        private int index;
        private float timer;
        private float duration;
        private SpriteRenderer spriteRenderer;
        private SpriteRenderer maskSpriteRenderer;
        private bool isInited;
        private bool isOpenMask;    // 是否改色

        // 新增合图自动播放接口
        public string atlasName;
        public string atlasKey;
        public string spriteName;
        public int frameSecond = 24;
        public string maskName;
        
        public void Init(SpriteListItem[] sprites, int framePerSecond = 24, SpriteListItem[] maskSprites = null)
        {
            this.sprites = sprites;
            this.maskSprites = maskSprites;
            this.duration = 1.0f / framePerSecond;
            this.timer = 0;
            this.index = 0;
            this.isInited = this.sprites != null && this.spriteRenderer != null && this.sprites.Length > 0;
            if (!this.isInited)
                return;

            bool isOpenMask = maskSprites != null;
            SetMaskOpen(isOpenMask);
            RefreshSprite();
        }

        private void Awake()
        {
            this.spriteRenderer = GetComponent<SpriteRenderer>();
            if (!string.IsNullOrEmpty(atlasName) && !string.IsNullOrEmpty(spriteName))
            {
                SpriteAtlasFrame.Instance.getSpriteAtlas(atlasName, atlasKey, spriteName, sprites =>
                {
                    if (sprites != null)
                    {
                        Init(sprites, frameSecond);
                    }
                });
            }
        }
        
        private void SetMaskOpen(bool isOpen)
        {
            this.isOpenMask = isOpen;

            if (this.maskSpriteRenderer == null)
            {
                foreach (Transform child in transform)
                {
                    // 尝试获取子节点上的 SpriteRenderer 组件
                    SpriteRenderer spriteRenderer = child.GetComponent<SpriteRenderer>();
                    if (spriteRenderer != null)
                    {
                        this.maskSpriteRenderer = spriteRenderer;
                        break;
                    }
                }
            }
            if(this.maskSpriteRenderer != null)
            {
                if (this.maskSpriteRenderer.gameObject.activeSelf != isOpen)
                {
                    this.maskSpriteRenderer.gameObject.SetActive(isOpen);
                }
            }
        }

        private void Update()
        {
            if (!this.isInited)
                return;

            timer += Time.deltaTime;
            if (timer >= duration)
            {
                timer = 0;
                if (++index >= this.sprites.Length)
                    index = 0;

                RefreshSprite();
            }
        }

        private void RefreshSprite()
        {
            this.spriteRenderer.sprite = this.sprites[this.index].tmpSprite;
            if (this.isOpenMask)
            {
                this.maskSpriteRenderer.sprite = this.maskSprites[this.index].tmpSprite;
            }
        }
        
        public void RefreshSpriteName(string m_spriteName)
        {
            this.spriteRenderer = GetComponent<SpriteRenderer>();
            if (!string.IsNullOrEmpty(atlasName) && !string.IsNullOrEmpty(m_spriteName))
            {
                SpriteAtlasFrame.Instance.getSpriteAtlas(atlasName, atlasKey, m_spriteName, sprites =>
                {
                    if (sprites != null)
                    {
                        spriteName = m_spriteName;
                        Init(sprites, frameSecond);
                    }
                });
            }
        }
        
        public void RefreshSpriteNameAndMask(string spriteName, string maskSpriteName)
        {
            this.isOpenMask = true;
            this.spriteRenderer = GetComponent<SpriteRenderer>();
            if (!string.IsNullOrEmpty(atlasName) && !string.IsNullOrEmpty(spriteName))
            {
                int finishCount = 0;
                SpriteAtlasFrame.Instance.getSpriteAtlas(atlasName, atlasKey, spriteName, sprites =>
                {
                    if (sprites != null)
                    {
                        this.spriteName = spriteName;
                        this.sprites = sprites;
                        finishCount++;
                        if (finishCount >= 2)
                        {
                            Init(this.sprites, frameSecond, this.maskSprites);
                        }
                    }
                });
                SpriteAtlasFrame.Instance.getSpriteAtlas(atlasName, atlasKey, maskSpriteName, maskSprites =>
                {
                    if (maskSprites != null)
                    {
                        this.maskName = maskSpriteName;
                        this.maskSprites = maskSprites;
                        finishCount++;
                        if (finishCount >= 2)
                        {
                            Init(this.sprites, this.frameSecond, this.maskSprites);
                        }
                    }
                });
            }
        }
        
        public void RefreshAtlasNameAndSpriteName(string m_atlasName, string m_atlasKey, string m_spriteName)
        {
            this.atlasKey = m_atlasKey;
            this.spriteRenderer = GetComponent<SpriteRenderer>();
            if (!string.IsNullOrEmpty(m_atlasName) && !string.IsNullOrEmpty(m_spriteName))
            {
                SpriteAtlasFrame.Instance.getSpriteAtlas(m_atlasName, atlasKey, m_spriteName, sprites =>
                {
                    if (sprites != null)
                    {
                        spriteName = m_spriteName;
                        Init(sprites, frameSecond);
                    }
                });
            }
        }

    }
}
